#ifndef GATE_SERVER_ROOT_H
#define GATE_SERVER_ROOT_H

#include "ExportDefine.h"
#include "singleton.h"
#include <gate_module.h>
#include <pipe_module.h>
#include <vector>

class CDynLib;
class IPlugin;
class IProxyEventHandlerFactory;
class EXPORT GateServerRoot : public CSingleton<GateServerRoot>
{
    friend class CSingleton<GateServerRoot>;
    typedef std::vector<CDynLib*> PluginLibList;
    typedef std::vector<IPlugin*> PluginInstanceList;
public:
    // default constructor
    GateServerRoot(void);

    // default destructor
    ~GateServerRoot(void);

    // Run in while loop
    bool RunOnce();

    // initialize
    bool Init();

    bool LoadPlugin(const std::string & strPluginName);

    void UnloadPlugin(const std::string & strPluginName);

    void InstallPlugin(IPlugin* plugin);

    void UninstallPlugin(IPlugin* plugin);

    void SetProxyEventHandlerFactory(IProxyEventHandlerFactory * pFactory);

    IProxyEventHandlerFactory * GetProxyEventHandlerFactory();

    utility::network::PIPEID GetDBServerPipeId();

    uint16 GetListenerPort();
private:
    // Initialize
    bool _Initialize();
    // Initialize log
    bool _Init_log();
    // Load configuration file
    bool _LoadConfig(const char * szFile);
    // Create the instance for each singleton object
    bool _CreateInstance();
    // Destroy all of the singleton objects
    void _DestroyInstance();
private:
    utility::network::net_module * m_pNetModule;
    utility::network::listener * m_pListener;
    utility::network::gate_module * m_pGateModule;
    utility::network::pipe_module * m_pPipeModule;
    IProxyEventHandlerFactory * m_pProxyEventHandlerFactory;

    std::string m_strPort;
    std::string m_strIP;
    std::string m_strEventHandlerFileName;
    PluginLibList m_PluginLibs;
    PluginInstanceList m_Plugins;
};

#endif // endif of GATE_SERVER_ROOT_H

